package com.jmes.web.listener;

import org.apache.ibatis.logging.LogFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.net.URL;

/**
 * 初始化Log4j.
 * User: jinshunlong
 * Date: 13-4-14
 * Time: 下午3:58
 */
public class Log4jListener implements ServletContextListener {

    /**
     * Notification that the web application initialization
     * process is starting.
     * All ServletContextListeners are notified of context
     * initialisation before any filter or servlet in the web
     * application is initialized.
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        URL url = Thread.currentThread().getContextClassLoader().getResource("log4j.xml");
        DOMConfigurator.configure(url);
        Logger.getLogger(Log4jListener.class);
        LogFactory.useLog4JLogging();
    }

    /**
     * Notification that the servlet context is about to be shut down. All servlets
     * have been destroy()ed before any ServletContextListeners are notified of context
     * destruction.
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
